package com.pishou;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pishou.code.bean.Cart;
import com.pishou.code.bean.Comment;
import com.pishou.code.bean.Product;
import com.pishou.code.csv.CategoryCSV;
import com.pishou.code.csv.ProductCSV;
import com.pishou.code.thread.DataDownload;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class Data {

    public static List<Product> products = new ArrayList<>();
    public static String[] categorys;
    public static List<Cart> carts = new ArrayList<>();
    public static Integer cartCount = 0;
    public static Double cartTotal = 0.00;
    public static List<Comment> comments = new ArrayList<>();
    private static DecimalFormat df = new DecimalFormat("###########0.00");
    public static List<Comment> comments_video = new ArrayList<>();
    public static boolean comment_ad_action = false;

    public static boolean startUp = false;

    public static List<Comment> current_product_comment_video = null;


    public static Product getProduct(Long sku){
        Product product = null;
        for (Product p:products){
            if (p.getSku().equals(sku)){
                product = p;
                break;
            }
        }
        return product;
    }
    public static boolean addCart(Product product){
        boolean flag = false;
        for (int i=0;i<Data.carts.size();i++){
            Cart cart = Data.carts.get(i);
            if (cart.getProduct().getSku().equals(product.getSku())){
                flag = true;
                cart.setQuantity(cart.getQuantity() + 1);
                break;
            }
        }
        if (!flag){
            Data.carts.add(new Cart(product,1));
        }
        countCart();
        return flag;
    }
    public static void reduceCart(int index){
        Cart cart = carts.get(index);
        if (cart.getQuantity()>1){
            cart.setQuantity(cart.getQuantity()-1);
            carts.set(index,cart);
        }else{
            carts.remove(index);
        }
        countCart();
    }
    public static void clearCart(){
        carts.clear();
        countCart();
    }
    private static void countCart(){
        Integer count = 0;
        Double total = 0.00;
        for (Cart cart:carts){
            count += cart.getQuantity();
            total += cart.getQuantity() * cart.getProduct().getPrice();
        }
        cartCount = count;
        cartTotal = Double.valueOf(df.format(total));
    }

    private static List<Comment> getCommentsBySku(Long sku){
        List<Comment> sku_comment = new ArrayList<>();
        for (Comment comment: comments) {
            if (sku.equals(comment.getSku())){
                sku_comment.add(comment);
            }
        }
        return sku_comment;
    }

    public static String getCartData_qrcode(){
        JSONObject qr_code = new JSONObject();
        String machineId = ParamConfig.getConfig_json().getString("machineId");
        qr_code.put("m_id", machineId);
        List<JSONObject> list = new ArrayList<>();
        for (Cart cart : carts){
            JSONObject c = new JSONObject();
            c.put("q",cart.getQuantity());
            c.put("s",cart.getProduct().getSku().toString());
            list.add(c);
        }
        qr_code.put("list",list);
        String result = URLConfig.url_cart_qrcode + "?data=" + JSON.toJSONString(qr_code);
        return result;
    }

    /**
     * 获取自动贩售机数据
     * @param updateFlag  是否直接从网络更新
     * @param wait  是否进行线程等待
     */
    public synchronized static void setData(boolean updateFlag, boolean wait){
        // 判断是否从存储中读取
        if(!updateFlag){
            products = ProductCSV.getProduct();
            categorys = CategoryCSV.getCategory();
        }
        // 若存储中为空，或选择直接从网络中更新
        if (products.size()<=0 || categorys.length <= 0 || updateFlag){
            Thread t = new DataDownload();
            t.start();
            // 是否进行线程等待
            if (wait){
                while (t.isAlive());
            }
        }
    }
}
